<?php
// Script to display a PHP class or lib manual using the Manta doc (Mdoc) class.
require("./config.php");

// Load required Manta classes
if ($force_php4 
    || version_compare(phpversion(), "5") < 0) {
  $mantalibpath = dirname(__FILE__) . "/../include/php4/";
} else {
  $mantalibpath = dirname(__FILE__) . "/../include/php5/";
}
require($mantalibpath. "Mdoc.php");

// Define the alternative file paths to the PHP code to document (relative to DOCUMENT_ROOT)
$alt_dir['manta'] = $mantalibpath;

// Set a reference file path
if (!empty($_GET['path']) && !empty($alt_dir[$_GET['path']])) {
  $ref_dir = $alt_dir[$_GET['path']];
  $path = $_GET['path'];
} else {
  $ref_dir = $_SERVER['DOCUMENT_ROOT'] . "/phpManta/Mdoc/samples/";
}

// Library or class file name is required
if (!empty($_GET['file'])) {
  $ref_file = $_GET['file'];
  $ref_file_path = $ref_dir . $ref_file . ".php";
} else {
  die("INVALID REQUEST");
}

// The tree view can be reduced to a branch
if (!empty($_GET['branch'])) {
  $view_branch = $_GET['branch'];
} else {
  $view_branch = "";
}

// Buid requested manual
$manual = new Mdoc($ref_file_path);
$tree_struct = print_r($manual->get_tree(), 1);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpManta - Mdoc - View parsing result</title>
</head>

<body>
<h1>View Mdoc parsing of <?php echo $ref_file; ?></h1>
<h2>Parser Log</h2>
<?php 
  // Display parser log if not empty
  if (!empty($manual->parser_log)) {
    echo "<pre style=\"color: #000099\">\n";
    echo "<b>Parsing errors:</b>\n";
    $manual->uparse_exportlog(true); 
    echo "</pre>";
  } else {
    echo "<p>Parsing completed with no error.</p>";
  }
?>
<h2>Documentation tree structure</h2>
<?php 
  $query = "file=$ref_file";
  $query .= empty($path) ? "" : "&path=$path";
  echo "<p><a href=\"view-internals.php?$query\">View documentation tree content.</a></p>\n";
  echo "<pre>";
  preg_match_all('/^\s*\[.*?\]/m', $tree_struct, $m);
  $clean_struct = implode("\n", $m[0]);
  echo $clean_struct;
  preg_match_all('/\[.*?\]/m', $tree_struct, $m);
  echo "</pre>";
?>
</body>
</html>
